package com.ruoyi.web.controller.gxs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.GxsECharts;
import com.ruoyi.gxs.domain.GxsPendActivity;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsPendAward;
import com.ruoyi.gxs.service.IGxsPendAwardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 待审批奖励Controller
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/gxs/award")
public class GxsPendAwardController extends BaseController
{
    @Autowired
    private IGxsPendAwardService gxsPendAwardService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询待审批奖励列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:list')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @GetMapping("/list")
    public TableDataInfo list(GxsPendAward gxsPendAward)
    {
        startPage();
        List<GxsPendAward> list = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);
        return getDataTable(list);
    }

    /**
     * 导出待审批奖励列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:export')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @Log(title = "待审批奖励", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsPendAward gxsPendAward)
    {
        List<GxsPendAward> list = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);
        ExcelUtil<GxsPendAward> util = new ExcelUtil<GxsPendAward>(GxsPendAward.class);
        util.exportExcel(response, list, "待审批奖励数据");
    }

    /**
     * 获取待审批奖励详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:query')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @GetMapping(value = "/{gxsPendAwardid}")
    public AjaxResult getInfo(@PathVariable("gxsPendAwardid") Long gxsPendAwardid)
    {
        return success(gxsPendAwardService.selectGxsPendAwardByGxsPendAwardid(gxsPendAwardid));
    }

    /**
     * 新增待审批奖励
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:add')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @Log(title = "待审批奖励", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsPendAward gxsPendAward)
    {

        //获奖用户id和学号和获奖用户名
        SysUser sysUser = sysUserService.selectUserById(getUserId());
        gxsPendAward.setUserId(sysUser.getUserId());
        gxsPendAward.setUserName(sysUser.getUserName());
        gxsPendAward.setNickName(sysUser.getNickName());
        gxsPendAward.setDeptId(sysUser.getDeptId());
        gxsPendAward.setGxsPendStatus(0L);

        //申请人、申请时间
       gxsPendAward.setCreateBy(sysUser.getNickName());
       gxsPendAward.setCreateTime(new Date());

        return toAjax(gxsPendAwardService.insertGxsPendAward(gxsPendAward));
    }

    /**
     * 修改待审批奖励
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:edit')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @Log(title = "待审批奖励", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsPendAward gxsPendAward)
    {
        return toAjax(gxsPendAwardService.updateGxsPendAward(gxsPendAward));
    }

    /**
     * 删除待审批奖励
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:remove')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @Log(title = "待审批奖励", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsPendAwardids}")
    public AjaxResult remove(@PathVariable Long[] gxsPendAwardids)
    {
        return toAjax(gxsPendAwardService.deleteGxsPendAwardByGxsPendAwardids(gxsPendAwardids));
    }

    /**
     * 查询我的奖励列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:list')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @GetMapping("/myList")
    public TableDataInfo myList(GxsPendAward gxsPendAward)
    {
        startPage();
        List<GxsPendAward> list = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);

        String username = getUsername();
        for (int i = 0; i <list.size();i++) {
            if (!list.get(i).getUserName() .equals(username)) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }


    /**
     * 查询待审批奖励列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:award:list')")
    @PreAuthorize("@ss.hasAnyRoles('class,admin,student,common')")
    @GetMapping("/pendList")
    public TableDataInfo pendList(GxsPendAward gxsPendAward)
    {
        startPage();
        List<GxsPendAward> list = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);

        Long NowDeptId = getDeptId();

        for (int i = 0; i < list.size(); i++){
            Long deptId = list.get(i).getDeptId();
            if (! deptId.equals(NowDeptId)){
                list.remove(i);
                i--;
            }
        }

        for (int i = 0; i <list.size();i++) {
            if (list.get(i).getGxsPendStatus() != 1 ) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }


    /**
     * 查询已审批奖励列表
     */
    @PreAuthorize("@ss.hasPermi('system:award:approvedList')")
    @GetMapping("/approvedList")
    public TableDataInfo approvedList(GxsPendAward gxsPendAward)
    {
        startPage();
        List<GxsPendAward> list = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);

        for (int i = 0; i <list.size();i++) {
            if (list.get(i).getGxsPendStatus() != 2 ) {
                list.remove(i);
            }
        }

        return getDataTable(list);
    }


    /**
     * 申请奖励
     */
    @PreAuthorize("@ss.hasPermi('gxs:award:query')")
    @GetMapping( "/applyAward/{gxsPendAwardid}")
    public AjaxResult applyAward(@PathVariable("gxsPendAwardid") Long gxsPendAwardid)
    {
        return success(gxsPendAwardService.selectApplyAwardid(gxsPendAwardid));
    }

    /**
     * 审批减分
     */
    @PreAuthorize("@ss.hasPermi('gxs:award:edit')")
    @Log(title = "待审批奖励", businessType = BusinessType.UPDATE)
    @PutMapping( "/examineAward")
    public AjaxResult examineAward(@RequestBody GxsPendAward gxsPendAward)
    {
        return toAjax(gxsPendAwardService.selectExamineAward(gxsPendAward));
    }


    /**
     * 本级活动分布
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/chartList")
    public TableDataInfo chartList(GxsPendAward gxsPendAward)
    {
        startPage();

        gxsPendAward.setDeptId(getDeptId());
        List<GxsPendAward> awardList = gxsPendAwardService.selectGxsPendAwardList(gxsPendAward);

        List<GxsECharts> list = new ArrayList<>();
        GxsECharts eCharts0 = new GxsECharts();
        GxsECharts eCharts1 = new GxsECharts();
        GxsECharts eCharts2 = new GxsECharts();

        eCharts0.setType("专业技能");
        eCharts0.setNum(0);;
        list.add(0,eCharts0);
        eCharts1.setType("思想品德");
        eCharts1.setNum(0);;
        list.add(1, eCharts1);
        eCharts2.setNum(0);;
        eCharts2.setType("体美劳");
        list.add(2,eCharts2);


        for (int i = 0 ; i < awardList.size(); i++){
            Long gxsTypeId = awardList.get(i).getGxsTypeId();
            switch ( gxsTypeId.intValue()){
                case 0:
                    Integer num0 = list.get(0).getNum();
                    list.get(0).setNum( num0 + 1);
                    break;
                case 1:
                    Integer num1 = list.get(1).getNum();
                    list.get(1).setNum( num1 + 1);
                    break;
                case 2:
                    Integer num2 = list.get(2).getNum();
                    list.get(2).setNum( num2 + 1);
                    break;
            }
        }

        return getDataTable(list);
    }
}
